# -*- coding: utf-8 -*-

import testcase

from stream.models import BookmarkEntry, Message, Tag, FeedChannel, DeliciousChannel
from stream import feeds, utils, models
import feedprovider

class LeanDbTestCase(testcase.TestCase):
    def login(self, openid_url):
        self.assertTrue(isinstance(openid_url, (str, unicode)))
        from openid.consumer.consumer import SUCCESS
        from django.contrib.auth import authenticate
        endpoint = utils.PropertyBag(getDisplayIdentifier = lambda: openid_url)
        openid_response = utils.PropertyBag(identity_url = openid_url, status = SUCCESS, endpoint = endpoint)
        self.client.login(openid_response = openid_response)

    def setUp(self):
        super(LeanDbTestCase, self).setUp()
        self.openid1 = 'http://user1.example.com'
        self.user1 = models.getOrCreateOpenIDUser(self.openid1)
        profile = self.user1.get_profile()
        profile.display_name = 'User One'
        profile.save()

        self.openid2 = 'http://user2.example.com'
        self.user2 = models.getOrCreateOpenIDUser(self.openid2)
        profile = self.user2.get_profile()
        profile.display_name = 'User Two'
        profile.save()

        self.feed_provider = feedprovider.FeedProvider()
        feeds.retrieve_feed.mock(self.feed_provider.retrieve_feed)
        feeds.find_feed.mock(self.feed_provider.find_feed)

    def tearDown(self):
        self.feed_provider.data_dict = {}
        self.feed_provider.discover_dict = {}
        super(LeanDbTestCase, self).tearDown()

class FilledDbTestCase(LeanDbTestCase):
    def setUp(self):
        """
        Initial application setup
        """
        super(FilledDbTestCase, self).setUp()

        self.search_tag = Tag(name = "search")
        self.search_tag.save()
        self.yahoo_tag = Tag(name = "yahoo")
        self.yahoo_tag.save()
        self.google_tag = Tag(name = "google")
        self.google_tag.save()
        self.cpp_tag = Tag(name = "c++")
        self.cpp_tag.save()
        self.c_tag = Tag(name = "c")
        self.c_tag.save()

        self.google_entry = BookmarkEntry(link = "http://google.com/")
        self.google_entry.save()

        self.google_message = Message(entry = self.google_entry, user = self.user1)
        self.google_message.save()
        self.google_message.tags.add(self.search_tag)
        self.google_message.tags.add(self.google_tag)
        self.google_message.save()

        self.yahoo_entry = BookmarkEntry(link = "http://yahoo.com/")
        self.yahoo_entry.save()

        self.yahoo_message = Message(entry = self.yahoo_entry, user = self.user2, published = True)
        self.yahoo_message.save()
        self.yahoo_message.tags.add(self.search_tag)
        self.yahoo_message.tags.add(self.yahoo_tag)
        self.yahoo_message.save()

        # The cpp_entry will be shared between the users
        self.cpp_entry = BookmarkEntry(link = "http://cplusplus.com/")
        self.cpp_entry.save()

        self.cpp_message1 = Message(entry = self.cpp_entry, user = self.user1)
        self.cpp_message1.save()
        self.cpp_message1.tags.add(self.cpp_tag)
        self.cpp_message1.save()

        self.cpp_message2 = Message(entry = self.cpp_entry, user = self.user2)
        self.cpp_message2.save()
        self.cpp_message2.tags.add(self.c_tag)
        self.cpp_message2.save()

        self.feed_provider.data_dict = feedprovider.feed_data
        self.feed_provider.discover_dict = feedprovider.feed_discover
        self.russian_channel = FeedChannel.get_or_create(url = 'http://yandex.ru')
        for entry in self.russian_channel.entries.all():
            entry.tags.clear()
            entry.save()

        self.codinghorror_channel = FeedChannel.get_or_create(url = 'http://codinghorror.com')

        # Subscribe users to different channel
        self.feed_subscription1 = self.russian_channel.createSubscription(self.user1)[0]
        self.feed_subscription2 = self.codinghorror_channel.createSubscription(self.user2)[0]
